//============================================================================
// Name        : cpp_timer.cpp
// Author      : darkflash
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <time.h>
#include <signal.h>
#include <unistd.h>

#include "getch.cpp"
using namespace std;

void setTimer(timer_t timerid, void (*Func)(int), time_t nsec)
{
	struct itimerspec value;
	struct sigevent av_sig_spec;

	av_sig_spec.sigev_notify = SIGEV_SIGNAL;
	av_sig_spec.sigev_signo =  SIGRTMIN;

	value.it_value.tv_sec = nsec;
	value.it_value.tv_nsec = 0;

	value.it_interval.tv_sec = nsec;
	value.it_interval.tv_nsec = 0;

	timer_create(CLOCK_REALTIME, &av_sig_spec, &timerid);
	timer_settime(timerid, 0, &value, NULL);

	signal(SIGRTMIN, Func);
}

void runTimer(int param)
{
	cout << "this is timer" << endl;
}

int main() {
	timer_t timerid;

	setTimer(timerid, runTimer, 3);

	cout << "press any key" << endl;
	getch();
	timer_delete(timerid);

	return 0;
}
